package com.ycw.servlet.guoguo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ycw.dao.LogisticsDao;
import com.ycw.dao.impl.LogisticsDaoImpl;

/**
 * Servlet implementation class ShowLogisticsByCondition
 */
@WebServlet("/showLogisticsByConditionServlet")
public class ShowLogisticsByConditionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * 根据条件name 和 phone 查询数据
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String l_name = request.getParameter("l_name");
		String l_phone = request.getParameter("l_phone");
		System.out.println(l_name);
		System.out.println(l_phone);
		// 分页查询第几页
		String page = request.getParameter("pageNum");
		if (page == null || "".equals(page)) {
			page = "1";
		}
		Integer pageNum = Integer.parseInt(page);

		LogisticsDao ld = LogisticsDaoImpl.getInstance();
		/**
		 * 这两个方法的执行顺序不可以变，否则的不到总页数
		 */
		/**
		 * 根据条件得到查询结果
		 */
		Map<String, Object> map = ld.queryAllLogisticsDataByNameOrPhone(l_name, l_phone, pageNum);
		/**
		 * 根据条件得到查询总数数目
		 */
		int Count = ld.queryAllLogisticsDataByNameOrPhone();
		// 计算总页数
		int pageCount = Count % 10 == 0 ? Count / 10 : Count / 10 + 1;
		map.put("Count", Count);
		map.put("pageCount", pageCount);
		String resJSON = JSON.toJSONString(map);
		PrintWriter out = response.getWriter();
		out.write(resJSON);

	}
}
